package com.ict.luxshare.jobservice;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import java.util.Set;

// RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)//API需要在21及以上

public class BtJobService extends JobService {
    private final static String TAG = "jb:BtJobService";
    public final static int REGISTER_BT_RECEIVER = 1;
    private static Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.d(TAG, "handleMessage() " + msg.what);

            switch (msg.what) {
                case REGISTER_BT_RECEIVER:
                    BtReceiver mBtReceiver = new BtReceiver();
                    IntentFilter btFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                    mContext.registerReceiver(mBtReceiver, btFilter);
                    mContext.registerReceiver(mBtReceiver, new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED));
                    break;
                default:
                    Log.d(TAG, "error:" + msg.what);
            }

            return true;
        }
    });

    /**
     * Override this method with the callback logic for your job. Any such logic needs to be
     * performed on a separate thread, as this function is executed on your application's main
     * thread.
     *
     * @param params Parameters specifying info about this job, including the extras bundle you
     *               optionally provided at job-creation time.
     * @return True if your service needs to process the work (on a separate thread). False if
     * there's no more work to be done for this job.
     */

    private static BluetoothManager mBluetoothManager;
    private static BluetoothAdapter mBluetoothAdapter;
    private static Context mContext;
    @Override
    public boolean onStartJob(JobParameters params) {
        //mBluetoothManager=(BluetoothManager)this.getSystemService(BLUETOOTH_SERVICE);
        //mBluetoothAdapter=mBluetoothManager.getAdapter();

        Log.d(TAG, "onStartJob(): " + params.getJobId());
        mContext = this;

        Intent intent = new Intent(mContext, Empty.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        ((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5 * 60 * 1000, pendingIntent);

        mHandler.sendMessage( Message.obtain( mHandler, params.getJobId(), params ) );
        return true;
    }

    /**
     * This method is called if the system has determined that you must stop execution of your job
     * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}.
     * <p>
     * <p>This will happen if the requirements specified at schedule time are no longer met. For
     * example you may have requested WiFi with
     * {@link JobInfo.Builder#setRequiredNetworkType(int)}, yet while your
     * job was executing the user toggled WiFi. Another example is if you had specified
     * {@link JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left its
     * idle maintenance window. You are solely responsible for the behaviour of your application
     * upon receipt of this message; your app will likely start to misbehave if you ignore it. One
     * immediate repercussion is that the system will cease holding a wakelock for you.</p>
     *
     * @param params Parameters specifying info about this job.
     * @return True to indicate to the JobManager whether you'd like to reschedule this job based
     * on the retry criteria provided at job creation-time. False to drop the job. Regardless of
     * the value returned, your job must stop executing.
     */
    @Override
    public boolean onStopJob(JobParameters params) {
        //mHandler.removeCallbacksAndMessages(null);
        Log.d(TAG, "onStopJob()");
        return false;
    }

}
